"""
cron: */30 * * * *
new Env('微博热搜采集');
"""
import requests
import os
import re
import mysql.connector
from datetime import datetime
from bs4 import BeautifulSoup

# 获取环境变量
host = os.environ.get('HOST')
user = os.environ.get('USER')
password = os.environ.get('PASSWORD')
database = os.environ.get('DATABASE')

print("正在获取环境变量...")
print("数据库地址：" + host)
print("数据库用户名：" + user)
print("数据库密码：" + password)
print("数据库名称：" + database)

# 发送HTTP请求获取微博热搜
print("发送HTTP请求获取微博热搜...")
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get('https://s.weibo.com/top/summary', headers=headers)

# 连接到MySQL数据库
print("连接到MySQL数据库...")
connection = mysql.connector.connect(
    host=host,
    user=user,
    password=password,
    database=database
)

cursor = connection.cursor()

# 设置要处理的ID范围（21-40用于微博热搜）
id_list = list(range(21, 41))

# 删除已有数据
print("删除已有数据...")
delete_query = "DELETE FROM ningm_news_list WHERE id IN ({})".format(", ".join(str(i) for i in id_list))
cursor.execute(delete_query)

# 解析并保存数据
print("解析并保存微博热搜数据...")
if response.status_code == 200:
    soup = BeautifulSoup(response.text, 'html.parser')
    hot_items = soup.select('.data tr')
    
    for index, item in enumerate(hot_items):
        if index >= 20:  # 只取前20条
            break
            
        title_element = item.select_one('.td-02 a')
        if not title_element:
            continue
            
        title = title_element.text.strip()
        url = 'https://s.weibo.com' + title_element['href']
        hot_value = item.select_one('.td-02 span')
        contant = hot_value.text if hot_value else '暂无热度'
        
        print(f"正在保存第{index + 1}条数据...")
        insert_query = "INSERT INTO ningm_news_list (id, title, url, create_time, contant, type) VALUES (%s, %s, %s, %s, %s, %s)"
        insert_values = (id_list[index], title, url, datetime.now(), contant, 'sina')
        cursor.execute(insert_query, insert_values)

# 提交更改并关闭连接
connection.commit()
cursor.close()
connection.close()

print("数据保存完成！") 